package com.member.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.member.command.ClazzCmd;
import com.member.controller.validation.clazzAddUpdate;
import com.member.exception.CustomException;
import com.member.po.Clazz;
import com.member.po.Page;
import com.member.service.ClazzService;

@Controller
@RequestMapping("/clazz")
public class ClazzController {
	@Autowired
	private ClazzService clazzService;
	
	@RequestMapping("/clazz")
	public  String clazz(ClazzCmd clazzCmd ,Model model,HttpServletRequest request){
			
		Map map=new HashMap();
		Page page = new Page();
		map.put("size", page.getSize());
		map.put("pageNum", page.getPageNum());
		
		if(clazzCmd.getPageNum()!=null){
			page.setPageNum(Integer.parseInt(clazzCmd.getPageNum()));
			map.put("pageNum", page.getPageNum());
		}
		if(clazzCmd.getClazzId()!=null){
			System.out.println("按clazzId");
			map.put("clazzId",Integer.parseInt(clazzCmd.getClazzId()));
		}
			
		List<Clazz> clazzList=clazzService.select(map);
		page.setAmontRow(clazzService.countRows(map));
		
		model.addAttribute("page", page);
		model.addAttribute("clazzList", clazzList);

		return "/baseInfo/clazz/clazzList";		
	}
	
	@ModelAttribute("clazzs")
	public List<Clazz> getClazzTypes() {		
		List<Clazz> clazzList;
		try {
			clazzList = clazzService.select(null);
			System.out.println("显示clazzs成功");
			System.out.println("size="+clazzList.size());
			return clazzList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/editClazz", method = { RequestMethod.POST,RequestMethod.GET })	
	public String editClazz(Model model,@RequestParam(value = "id", required = true) Integer items_id)
			throws Exception {
		Clazz clazz=clazzService.selectById(items_id);			
		if(clazz == null){
			throw new CustomException("修改的班级信息不存在!");
		}	
		model.addAttribute("items", clazz);
		return "/baseInfo/clazz/clazzUpdate";
	}
	
	@RequestMapping("/updateClazzSubmit")
	public String updateClazzSubmit(Model model,	HttpServletRequest request,	Integer id,
		@Validated(value={clazzAddUpdate.class}) Clazz clazz ,BindingResult result )
	{
		if(result.hasErrors())
		{
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
			model.addAttribute("items", clazz);
			return "/baseInfo/clazz/clazzUpdate";
		}
		try {
			clazzService.updateById(clazz);
			return "redirect:/clazz/clazz";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	
	}
	
	@RequestMapping("/addClazz")	
	public String addClazz()
	{	     
		return "/baseInfo/clazz/clazzAdd";
	}
	
	@RequestMapping(value = "/addClazzSubmit")	
	public String addClazzSubmit(Model model,
			@Validated (value={clazzAddUpdate.class}) Clazz clazz,BindingResult result)
	{	
		if(result.hasErrors())
		{
			
			List<ObjectError> allErrors = result.getAllErrors();
			model.addAttribute("allErrors", allErrors);
		    model.addAttribute("items", clazz);
		    return "/baseInfo/clazz/clazzAdd";
		}
		
		try {
			clazzService.insert(clazz);
			
			return "redirect:/clazz/clazz";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping(value = "/deleteClazz")
	public String deleteClazz(Model model,@RequestParam(value = "id", required = true) Integer items_id)
	{
		try {
			clazzService.deleteById(items_id);
			
			return "redirect:/clazz/clazz";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
	
	@RequestMapping("/deleteClazzs")
	public String deleteClazzs(Model model, Integer[] id){
		if(id != null){
			for(Integer i:id){
				clazzService.deleteById(i);
			}
			return "redirect:/clazz/clazz";
		}
		return "error";
	}
	
}
